package com.ruoyi.system.api.produce.service.impl;

import com.ruoyi.common.core.utils.ConvertUtil;
import com.ruoyi.system.api.model.dto.SysConfigDto;
import com.ruoyi.system.api.service.ISysConfigApiService;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SysConfigApiService implements ISysConfigApiService {
    @Autowired
    private ISysConfigService configService;

    @Override
    public SysConfigDto getConfigByKey(String key) {
        SysConfig config = configService.selectConfigInfoByKey(key);
        SysConfigDto dto = ConvertUtil.sourceToTarget(config, SysConfigDto.class);
        return dto;
    }

    @Override
    public void editSysConfig(SysConfigDto dto) {
        SysConfig sysConfig = ConvertUtil.sourceToTarget(dto, SysConfig.class);
        configService.updateConfig(sysConfig);
    }

    @Override
    public List<SysConfigDto> getConfigByKeyList(String[] keys) {
        List<SysConfig> sysConfigs = configService.selectConfigInfoByKey(keys);
        return ConvertUtil.sourceToTarget(sysConfigs, SysConfigDto.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchEditSysConfig(Map<String, String> map) {
        for (String key : map.keySet()) {
            configService.batchEditSysConfig(key, map.get(key));
        }
    }
}
